package shangji;

import org.dom4j.*;
import org.dom4j.io.HTMLWriter;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.Iterator;
import java.util.List;

public class Shangji3 {
    public static void main(String[] args) throws IOException, DocumentException {
        // update();
        // add();
        // delete();
    }
    public static void add() throws IOException {
        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/shangji/Shangji3.xml");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 创建Brand节点
        Element rootElement = doc.getRootElement();
        Element brand = DocumentHelper.createElement("Brand");
        brand.addAttribute("name","OPPO");

        Element type = DocumentHelper.createElement("Type");
        type.addAttribute("name","R11");

        brand.add(type);

        rootElement.add(brand);

        XMLWriter writer = new HTMLWriter(new FileWriter("src/shangji/Shangji3.xml"));
        writer.write(doc);
        System.out.println("新增成功");
        writer.close();
    }
    public static void update() throws IOException {
        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/shangji/Shangji3.xml");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 修改节点属性
        Element root = doc.getRootElement();
        List brand = root.elements("Brand");
        for (Iterator it= brand.listIterator();it.hasNext();) {  //遍历修改
            Element element = (Element) it.next();
            if (element.getName().equals("Brand")){
                if (element.attributeValue("name").equals("三星")){
                    element.addAttribute("name","SAMSUNG");
                    System.out.println("修改成功");
                }
            }
        }
        XMLWriter writer = new HTMLWriter(new FileWriter("src/shangji/Shangji3.xml"));
        writer.write(doc);

        writer.close();
    }
    public static void delete() throws IOException {
        Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/shangji/Shangji3.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 删除节点
        Element root = doc.getRootElement();
        List brand = root.elements("Brand");
        for (Iterator it = brand.listIterator(); it.hasNext(); ) {
            Element element = (Element) it.next();
            if (element.getName().equals("Brand")) {
                if (element.attributeValue("name").equals("SAMSUNG")){
                    Element parentElm = element.getParent();
                    parentElm.remove(element);
                    System.out.println("删除成功");
                }
            }
        }
        XMLWriter writer = new HTMLWriter(new FileWriter("src/shangji/Shangji3.xml"));
        writer.write(doc);
        writer.close();
    }
}
